package presentation.menuForm_UI;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.Timer;

public class Window extends JWindow {

	private static final long serialVersionUID = 1L;
	private int count = 0;
	private Timer timer;
	private Picture banner = new Picture();
	
	private JProgressBar progress = new JProgressBar();
	private JLabel lblProgress = new JLabel("");
	private ArrayList<File> listFile = new ArrayList<File>();
	
	public Window(){
		
		windowEvent();
		this.setOpacity(0.9f);
		this.setSize(786, 377);
		this.setLocationRelativeTo(null);
		
		this.setLayout(null);
		this.add(banner);
		this.setFocusable(false);
		banner.setBounds(0, 0, 786, 377);
		setMainLayout();
		
		banner.path = "src/image/banner.png";
		banner.repaint();
		
		getDefaultListFile("src");
		
		timer = new Timer(30, action);
		timer.start();
	}
	
	private void windowEvent()
	{
		this.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosed(WindowEvent evt)
			{
				new MenuForm_UI();
			}
		});
	}
	
	private ActionListener action = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			lblProgress.setText(listFile.get(count++).toPath().toString());
			progress.setValue(count);
			if(count == listFile.size() - 1)
			{
				timer.stop();
				dispose();
			}
		}
	};
	
	private void setMainLayout()
	{
		setLblProgressProperties();
		banner.setLayout(null);
		banner.setOpaque(false);
		banner.add(progress);
		banner.add(lblProgress);
		
		lblProgress.setBounds(5,335,776,20);
		progress.setBounds(lblProgress.getX(), lblProgress.getY() + lblProgress.getHeight(), lblProgress.getWidth(), lblProgress.getHeight());
	}
	
	private void setLblProgressProperties()
	{
		lblProgress.setFont(new Font("Calibri",0,15));
	}
	
	private void getDefaultListFile(String path)
	{
		File[] listFiles = new File(path).listFiles();
		for(int i =0; i<listFiles.length; i++)
		{
			if(listFiles[i].isFile())
			{
				listFile.add(listFiles[i]);
			}
			else if(listFiles[i].isDirectory())
			{
				getDefaultListFile(listFiles[i].toPath().toString());
			}
		}
	}
	
}
